angular.module("myillust", ['commonApp'])
.run(["$rootScope", ($rootScope) ->
    $rootScope.target = ""
    ])
.config(['$routeProvider', ($routeProvider) ->
    $routeProvider
    .when '/illust/my',
        templateUrl: '/public/tpl/myillust.html'
        controller: 'myillustCtrl'
    .when '/illust/my/:page',
        templateUrl: '/public/tpl/myillust.html'
        controller: 'myillustCtrl'
    .when '/illust/fav',
        templateUrl: '/public/tpl/fav.html'
        controller: 'favCtrl'
    ])
.filter('splittags', ->
    (tags) ->
        if $.trim(tags) != ''
            console.log (tags).split(",")
        )
.directive('tags', [() ->
    restrict: 'EA'
    scope: {
        taglists: '@'
    }
    template: '<span><ul class="tags"><li ng-repeat="tag in tags"><a>{{tag}}</a></li></ul></span>'
    link: (scope, ele, attrs, ctrl) ->
        attrs.$observe('taglists', (val) ->
            scope.tags = val.split(",")
            )
    ])
.controller('myillustCtrl', ['$rootScope','$scope','$timeout', '$http', '$routeParams', 'showMsg', '$location', ($rootScope, $scope, $timeout, $http, $routeParams ,showMsg, $location) -> 
    $scope.lists = []
    $scope.currentPage =  $scope.page = Number($routeParams.page)||1
    # console.log $scope.page, $scope.currentPage
    $scope.$watch 'currentPage', (newPage) ->
        # if Number(newPage) != 1
        #     $location.path("/illust/my/#{newPage}")
        $http.get("/illust/mydata/ac/mypic/p/#{newPage}").success (data) ->
            
            # console.log 
            $scope.lists = data.data
            # $scope.lists = $scope.lists.concat data.data # 连贯模式
            $rootScope.piccount = data.count
            $scope.bcs = data.bcs || "/"
            # console.log data
            $scope.noOfPages = data.page
            $("html, body").animate scrollTop: '0', 300
            # if 
    $scope.dichange = (data, pid)->
        $http.get("/illust/cinfo/ac/di/pid/#{pid}/data/#{data}").success (data) ->
            if data.status == 1
                showMsg "修改成功", 3500, "pull-right wb"
    $scope.ratedchange = (data, pid)->
        $http.get("/illust/cinfo/ac/rated/pid/#{pid}/data/#{data}").success (data) ->
            if data.status == 1
                showMsg "修改成功", 3500, "pull-right wb"
    $scope.del = (id) ->
        if confirm "是否要删除此图片"
            # alert "此图片已经删除"
            $http.get("/illust/mydel/id/#{id}").success (data) ->
                if data.status == 1
                    # console.log 
                    $("#i_#{id}").fadeOut();
                    showMsg "删除成功", 3500, 'pull-right wb'
                    $timeout(->
                        $("#i_#{id}").remove()
                    ,1000)
                else 
                    console.log data
                    showMsg "删除失败", 3500, 'pull-right wb'
    ])
.controller('favCtrl', ['$rootScope','$scope','$timeout', '$http', 'showMsg', '$routeParams', ($rootScope,$scope, $timeout, $http, showMsg, $routeParams) -> 
    $scope.lists = []
    $scope.currentPage =  $scope.page = Number($routeParams.page)||1
    # console.log $scope.page, $scope.currentPage
    $scope.$watch 'currentPage', (newPage) ->
        # if Number(newPage) != 1
        #     $location.path("/illust/my/#{newPage}")
        $http.get("/illust/mydata/ac/fav/p/#{newPage}").success (data) ->
            
            # console.log 
            $scope.lists = data.data
            # $scope.lists = $scope.lists.concat data.data # 连贯模式
            $rootScope.piccount = data.count
            $scope.bcs = data.bcs || "/"
            # console.log data
            $scope.noOfPages = data.page
            $("html, body").animate scrollTop: '0', 300
    $scope.unlike = (id) ->
        # if confirm "是否要删除此图片"
            # alert "此图片已经删除"
        $http.get("/illust/like/id/#{id}").success (data) ->
            if data == '2'
                # console.log 
                $("#i_#{id}").fadeOut();
                showMsg "已经取消标记", 3500, 'pull-right wb'
                $timeout(->
                    $("#i_#{id}").remove()
                ,1000)
            else 
                # console.log (data == 2)
                showMsg "操作失败", 3500, 'pull-right wb'
    ])